<?php


namespace app\api\controller;
header("Access-Control-Allow-Origin:*");
header("Access-Control-Allow-Methods:GET, POST, OPTIONS, DELETE");
header("Access-Control-Allow-Headers:DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type, Accept-Language, Origin, Accept-Encoding");


use app\api\controller\Jwt as Jwt;
use think\Controller;
use think\Db;

class Aorder extends Controller
{
    private static $uid;

//    public function _initialize()
//    {
//        parent::_initialize(); // TODO: Change the autogenerated stub
//        $token = input('server.HTTP_AUTHORIZATION');
//
//        $result = Jwt::verifyToken($token);
//        if (!$result) {
//            echo json_encode(["code" => 400102, "msg" => '请先登录'], JSON_UNESCAPED_UNICODE);
//            die();
//        }
//        self::$uid = $result['sub'];   //给类的静态属性赋值 用户id
//    }

    /**
     *后台获取用户列表
     *
     * dev.ziwudiche.com/api/Aorder/getusers
     *
     */
    public function getusers()
    {
        $user_data = Db::name('user')->select();
        if ($user_data) {
            echo json_encode(["code" => 200, "data" => $user_data], JSON_UNESCAPED_UNICODE);
            die();
        } else {
            echo json_encode(["code" => 400, "msg" => '获取用户失败'], JSON_UNESCAPED_UNICODE);
            die();
        }
    }


    /**
     *后台获取订单列表
     *
     * dev.ziwudiche.com/api/Aorder/getorders
     *$status  1=代付款 2=待验车 3=待收车 4=已完成
     */
    public function getorders()
    {
        $status=input('status');
        if (!$status){
            $order_data = Db::name('order')->select();
//            echo "<pre>";
//            die(var_dump($order_data));
//            echo "</pre>";
        }else{
            $order_data = Db::name('order')->where('status',$status)->select();
        }
        if ($order_data || $order_data==[]) {
            echo json_encode(["code" => 200, "data" => $order_data], JSON_UNESCAPED_UNICODE);
            die();
        } else {
            echo json_encode(["code" => 404, "msg" => '获取订单失败'], JSON_UNESCAPED_UNICODE);
            die();
        }
    }

    /**
     *获取订单详情
     *
     * dev.ziwudiche.com/api/Aorder/getorderdetail
     * @param string $id 订单oid
     *
     */
    public function getorderdetail()
    {
        $oid = input('oid');
        Db::startTrans();
        try{
            $order_data = Db::name('order')->where('id', $oid)->find();
//            echo "<pre>";
//            die(var_dump($order_data));
//            echo "</pre>";
            $departureid= $order_data['departureid'];
            $receiverid=$order_data['receiverid'];
            $vehicle_data = Db::name('vehicle_information')->where('oid', $oid)->select();
            $departure_data = Db::name('address_information')->where('id', $departureid)->select();
            $receiver_data = Db::name('address_information')->where('id', $receiverid)->select();
            // 提交事务
            Db::commit();
            echo json_encode(["code" => 200, "order_data" => $order_data, "vehicle_data" => $vehicle_data, "departure_data" => $departure_data,"receiver_data" => $receiver_data], JSON_UNESCAPED_UNICODE);
            die();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            echo json_encode(["code" => 400, "msg" => '获取订单详情失败'], JSON_UNESCAPED_UNICODE);
            die();
        }
    }

    public function  test(){
        $oid = input('oid');
        $data= Db::name('vehicle_information')->where('id', $oid)->select();
        echo "<pre>";
        die(var_dump($data));
        echo "</pre>";
        $a=null;
        foreach ($data as $k => $v){
            $a[$k]['brand']=$v['brand'];
            $a[$k]['separation']=$v['separation'];
            $a[$k]['departure_mode']=$v['departure_mode'];
            $a[$k]['receiving_mode']=$v['receiving_mode'];
            $a[$k]['frame_number']=$v['frame_number'];
//            $a[$k]['start']=$v['start'];
//            $a[$k]['end']=$v['end'];
            $a[$k]['price']=$v['money'];
        }

        $db=Db::connect('mysql://csk:chen123@@rdsb6zoy8313n3f88o8j1o.mysql.rds.aliyuncs.com:3306/carrier#utf8');
        $result=$db->table('hb_cars')->insertAll($a);
        echo "<pre>";
        die(var_dump($result));
        echo "</pre>";
    }

    /**
     *后台更改订单状态
     * @param string $oid 订单id
     * @param string $status 订单状态:1=待确认,2=运送中,3=已完成)
     * dev.ziwudiche.com/api/Aorder/changestatus
     *
     */
    public function changestatus()
    {
        $oid = input('oid');
        $status = input('status');
        $res = Db::name('order')->where('id', $oid)->update(['status' => $status]);
        if ($res) {
            if ($status==2){
                $data= Db::name('vehicle_information')->where('oid', $oid)->select();
                $money=Db::name('order')->where('id', $oid)->value('price');
                $a['price']=$money;
                $a['start']='黄山市';
                    $a['end']='上海市';


                foreach ($data as $k => $v){
                    $a['brand']=$v['brand'];
                    $a['separation']=$v['separation'];
                    $a['departure_mode']=$v['departure_mode'];
                    $a['receiving_mode']=$v['receiving_mode'];
                    $a['frame_number']=$v['frame_number'];
//                    $a['start']=$v['start'];
//                    $a['end']=$v['end'];
//                    $a['price']=$v['money'];
                }
//                echo "<pre>";
//                die(var_dump($a));
//                echo "</pre>";

                $db=Db::connect('mysql://csk:chen123@@rdsb6zoy8313n3f88o8j1o.mysql.rds.aliyuncs.com:3306/carrier#utf8');
                $result=$db->table('hb_cars')->insert($a);
                if ($result){
                    echo json_encode(["code" => 200, "msg" => "更新成功"], JSON_UNESCAPED_UNICODE);
                    die();
                }else{
                    echo json_encode(["code" => 400, "msg" => "物流订单更新失败"], JSON_UNESCAPED_UNICODE);
                    die();
                }

            }else{
                echo json_encode(["code" => 200, "msg" => "更新成功"], JSON_UNESCAPED_UNICODE);
                die();
            }

        } else {
            echo json_encode(["code" => 404, "msg" => "更新失败"], JSON_UNESCAPED_UNICODE);
            die();
        }

    }



}